<?php

if (!defined('_PS_ADMIN_DIR_'))
    define('_PS_ADMIN_DIR_', getcwd());
include(_PS_ADMIN_DIR_ . '/../config/config.inc.php');

/* Getting cookie or logout */
require_once(_PS_ADMIN_DIR_ . '/init.php');

$context = Context::getContext();
switch (Tools::getValue('method')) {
    case 'addContainer' :
        // die( Tools::jsonEncode( array('result' => 'my_value')));
        // check of duplicate shelf
        $id_shelf = 0;
        $id_bin = 0;
        $id_shelf = Tools::getValue('id_shelf');
        $shelf_list = Tools::getValue('shelf_list');
        if ($id_shelf && $shelf_list == -1) {
            if (isDuplicateShelf()) {
                $result = array(
                    'status' => false,
                    'error' => 'The shelf is existing for selected warehouse!'
                );
                echo Tools::jsonEncode($result);
                break;
            } else {
                //add shelf
                $id_shelf = addShelf();
            }
        }
        $id_bin = Tools::getValue('id_bin');
        $bin_list = Tools::getValue('bin_list');
        if ($id_bin && $bin_list == -1) {
            // check of duplicate bin

            if (isDuplicateBin()) {
                $result = array(
                    'status' => false,
                    'error' => 'The bin is existing for selected shelf!'
                );
                echo Tools::jsonEncode($result);
                break;
            } else {//add bin
                $id_bin = addBin($id_shelf);
            }
        }
        $id_container = Tools::getValue('id_container');
        $container_list = Tools::getValue('container_list');
        if ($id_container && $container_list == -1) {

            // check of duplicate bin
            if (isDuplicateContainer()) {
                $result = array(
                    'status' => false,
                    'error' => 'The container is existing for selected bin!'
                );
                echo Tools::jsonEncode($result);
                break;
            } else {//add container
                $id_container = addContainer($id_bin);
            }
        }
        echo Tools::jsonEncode(array('message' => 'Adding Shelf/Bin/Container successfully!', 'status' => true));
        break;
    case 'deleteShelf':
        echo deleteShelf();
        break;
    case 'deleteBin':
        echo deleteBin();
        break;
    case 'deleteContainer':
        echo deleteContainer();
        break;
    case 'getBinList':
        echo getJSONBinList();
        break;
    case 'getContainerList':
        echo getJSONContainerList();
        break;
    default:
        exit;
}

function isDuplicateShelf() {
    $id_warehouse = Tools::getValue('id_warehouse');
    $shelf = Tools::getValue('id_shelf');
    $shelf_list = Tools::getValue('shelf_list');
    $bin = Tools::getValue('id_bin');
    $bin_list = Tools::getValue('bin_list');
    $container = Tools::getValue('id_container');
    $container_list = Tools::getValue('container_list');
    $sql = "SELECT COUNT(*) FROM " . _DB_PREFIX_ . "warehouse_shelf WHERE id_warehouse='" . pSQL($id_warehouse) . "' AND name='" . pSQL($shelf) . "'";
    $total = Db::getInstance()->getValue($sql);
    if ($total > 0) {
        return true;
    } else {
        return false;
    }
}

function isDuplicateBin() {
    $id_warehouse = Tools::getValue('id_warehouse');
    $shelf = Tools::getValue('id_shelf');
    $shelf_list = Tools::getValue('shelf_list');
    $bin = Tools::getValue('id_bin');
    $bin_list = Tools::getValue('bin_list');
    $container = Tools::getValue('id_container');
    $container_list = Tools::getValue('container_list');
    $sql = "SELECT COUNT(*) FROM " . _DB_PREFIX_ . "warehouse_bin WHERE id_shelf='" . pSQL($shelf) . "' AND name='" . pSQL($bin) . "'";
    $total = Db::getInstance()->getValue($sql);
    if ($total > 0) {
        return true;
    } else {
        return false;
    }
}

function isDuplicateContainer() {
    $id_warehouse = Tools::getValue('id_warehouse');
    $shelf = Tools::getValue('id_shelf');
    $shelf_list = Tools::getValue('shelf_list');
    $bin = Tools::getValue('id_bin');
    $bin_list = Tools::getValue('bin_list');
    $container = Tools::getValue('id_container');
    $container_list = Tools::getValue('container_list');
    $sql = "SELECT COUNT(*) FROM " . _DB_PREFIX_ . "warehouse_container WHERE id_bin='" . pSQL($bin) . "' AND name='" . pSQL($container) . "'";
    $total = Db::getInstance()->getValue($sql);
    if ($total > 0) {
        return true;
    } else {
        return false;
    }
}

function getJSONBinList() {
    $id_shelf = Tools::getValue('id_shelf', 0);
    $shelves = array();
    $sql = "SELECT id_bin,name FROM " . _DB_PREFIX_ . "warehouse_bin WHERE id_shelf='" . pSQL($id_shelf) . "'";

    if ($results = Db::getInstance()->ExecuteS($sql)) {
        $shelves = $results;
    }
    return json_encode($results);
}

function getJSONContainerList() {
    $id_bin = Tools::getValue('id_bin', 0);
    $shelves = array();
    $sql = "SELECT id_container,name FROM " . _DB_PREFIX_ . "warehouse_container WHERE id_bin='" . pSQL($id_bin) . "'";
    if ($results = Db::getInstance()->ExecuteS($sql)) {
        $shelves = $results;
    }
    return json_encode($results);
}

function deleteContainer() {
    $container = Tools::getValue('id_container');
    if (!$container) {
        $result = array(
            'status' => false,
            'error' => 'Please select a shelf!'
        );
        return json_encode($result);
    }
    if (containerIsUsed($container)) {
        $result = array(
            'status' => false,
            'error' => 'Container is in use! Please please remove the associated product to this container first!'
        );
        return json_encode($result);
    }
    Db::getInstance()->delete('warehouse_container', 'id_container=' . pSQL($container));
    $result = array(
        'status' => true,
        'error' => 'Delete container successfully!'
    );
    return json_encode($result);
}

function deleteShelf() {
    $shelf = Tools::getValue('id_shelf');
    if (!$shelf) {
        $result = array(
            'status' => false,
            'error' => 'Please select a shelf!'
        );
        return json_encode($result);
    }
    // check if the shel is in using
    if (shelfIsUsed($shelf)) {
        $result = array(
            'status' => false,
            'error' => 'Shelf is in use! Please please remove the associated product to this shelf first!'
        );
        return json_encode($result);
    }
    // check shelf has bin
    if (shelfHasBin($shelf)) {
        $result = array(
            'status' => false,
            'error' => 'Shelf has bins! Please delete bins first!'
        );
        return json_encode($result);
    } else {
        Db::getInstance()->delete('warehouse_shelf', 'id_shelf=' . pSQL($shelf));
        $result = array(
            'status' => true,
            'error' => 'Delete shelf successfully!'
        );
        return json_encode($result);
    }
}

function shelfIsUsed($shelf) {
    $sql = 'SELECT COUNT(*) FROM ' . _DB_PREFIX_ . 'warehouse_product_location WHERE id_shelf=' . pSQL($shelf);
    $total = Db::getInstance()->getValue($sql);
    if ($total > 0) {
        return true;
    } else {
        return false;
    }
}

function binIsUsed($bin) {
    $sql = 'SELECT COUNT(*) FROM ' . _DB_PREFIX_ . 'warehouse_product_location WHERE id_bin=' . pSQL($bin);
    $total = Db::getInstance()->getValue($sql);
    if ($total > 0) {
        return true;
    } else {
        return false;
    }
}

function containerIsUsed($container) {
    $sql = 'SELECT COUNT(*) FROM ' . _DB_PREFIX_ . 'warehouse_product_location WHERE id_container=' . pSQL($container);
    $total = Db::getInstance()->getValue($sql);
    if ($total > 0) {
        return true;
    } else {
        return false;
    }
}

function deleteBin() {
    $bin = Tools::getValue('id_bin');
    if (binIsUsed($bin)) {
        $result = array(
            'status' => false,
            'error' => 'Bin is in use! Please please remove the associated product to this bin first!'
        );
        return json_encode($result);
    }
    if (binHasContainer($bin)) {
        $result = array(
            'status' => false,
            'error' => 'Bin has containers! Please delete containers first!'
        );
        return json_encode($result);
    } else {
        Db::getInstance()->delete('warehouse_bin', 'id_bin=' . pSQL($bin));
        $result = array(
            'status' => true,
            'error' => false,
            'message' => 'Delete bin successfully!'
        );
        return json_encode($result);
    }
}

function shelfHasBin($id_shelf) {
    $sql = 'SELECT COUNT(*) FROM ' . _DB_PREFIX_ . 'warehouse_bin WHERE id_shelf=' . pSQL($id_shelf);
    $total = Db::getInstance()->getValue($sql);
    if ($total > 0) {
        return true;
    } else {
        return false;
    }
}

function binHasContainer($id_bin) {
    $sql = 'SELECT COUNT(*) FROM ' . _DB_PREFIX_ . 'warehouse_container WHERE id_bin=' . pSQL($id_bin);
    $total = Db::getInstance()->getValue($sql);
    if ($total > 0) {
        return true;
    } else {
        return false;
    }
}

function addShelf() {
    $id_warehouse = Tools::getValue('id_warehouse');
    $shelf = Tools::getValue('id_shelf');
    $shelf_list = Tools::getValue('shelf_list');
    $bin = Tools::getValue('id_bin');
    $bin_list = Tools::getValue('bin_list');
    $container = Tools::getValue('id_container');
    $container_list = Tools::getValue('container_list');
    // check for duplicate shelf
    if ($shelf_list == -1) {
        Db::getInstance()->insert('warehouse_shelf', array(
            'id_warehouse' => (int) $id_warehouse,
            'name' => pSQL($shelf),
        ));
        $id_shelf = Db::getInstance()->Insert_ID();
    } else {
        $id_shelf = $shelf;
    }
    return $id_shelf;
}

function addBin($id_shelf) {
    $id_warehouse = Tools::getValue('id_warehouse');
    $shelf = Tools::getValue('id_shelf');
    $bin = Tools::getValue('id_bin');
    $bin_list = Tools::getValue('bin_list');
    $container = Tools::getValue('id_container');
    if ($bin_list == -1) {
        Db::getInstance()->insert('warehouse_bin', array(
            'id_shelf' => (int) $id_shelf,
            'name' => pSQL($bin),
        ));
        $id_bin = Db::getInstance()->Insert_ID();
    } else {
        $id_bin = $bin;
    }
    return $id_bin;
}

function addContainer($id_bin) {
    $id_warehouse = Tools::getValue('id_warehouse');
    $shelf = Tools::getValue('id_shelf');
    $bin = Tools::getValue('id_bin');
    $container = Tools::getValue('id_container');
    $container_list = Tools::getValue('container_list');
    if ($container_list == -1) {
        Db::getInstance()->insert('warehouse_container', array(
            'id_bin' => (int) $id_bin,
            'name' => pSQL($container),
        ));
        $id_container = Db::getInstance()->Insert_ID();
    } else {
        $id_container = $container;
    }
    return $id_container;
}

exit;
?>